﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ActiproSoftware.SyntaxEditor;
using Microsoft.VisualStudio.Debugger.Interop;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio;
using PowerGui.AddOn.Impl;
using PowerGuiVsx.Interfaces;
using Quest.PowerGUI.Engine.Shell;
using Quest.PowerGUI.ScriptEditor.Shared;
using Quest.PowerGUI.ScriptEditor.Shared.Debugger;

namespace PowerGuiVsx.Core
{
    /// <summary>
    /// Manages events coming from the debug engine.
    /// </summary>
    public class DebugEventManager : IVsDebuggerEvents, IDebugEventCallback2
    {
        #region Fields
        private IEditorFactory _factory;
        #endregion

        public ConfigLoader ConfigLoader { get; set; }
        public PowerShellDebugger Debugger { get; set; }

        public DebugEventManager(IEditorFactory factory, ConfigLoader configLoader, PowerShellDebugger debugger)
        {
            _factory = factory;
            ConfigLoader = configLoader;
            Debugger = debugger;
        }

        #region Methods

        public int OnModeChange(DBGMODE dbgmodeNew)
        {
            return VSConstants.S_OK;
        }

        private bool set;
        public int Event(IDebugEngine2 pEngine, IDebugProcess2 pProcess, IDebugProgram2 pProgram, IDebugThread2 pThread, IDebugEvent2 pEvent, ref Guid riidEvent, uint dwAttrib)
        {
            if (pEvent is IPowerShellProgramCreated && !set)
            {
                Trace.WriteLine("Setting the UI for the debugger.");
                set = true;
                var debugger = (pEvent as IPowerShellProgramCreated).Engine as IDebugEngine;
                debugger.ProvideComponents(ConfigLoader.Shell, ConfigLoader.SettingsManager, Debugger.Debugger, _factory);
            }

            return VSConstants.S_OK;
        }



        #endregion
    }
}
